//
//  HomeViewController.swift
//  weibosina
//
//  Created by hwm on 16/8/28.
//  Copyright © 2016年 hwm. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    let homeCellID = "homeCell"
    // 保存微博数组
    var statuses: [Status]?
        {
        didSet{
            // 当别人设置完毕数据, 就刷新表格
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 1.如果没有登录, 就设置未登录界面的信息
        if !isLogin
        {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        // 2.初始化导航条
        setupNav()
        
        tableView.backgroundColor = UIColor(red:0.85, green:0.85, blue:0.85, alpha:1.00)
        
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: homeCellID)
//        setTab()
        
        // MARK: 请求数据
        loadData()

    }
    
    private func setTab() {
        
        tableView.backgroundColor = UIColor(red:0.85, green:0.85, blue:0.85, alpha:1.00)
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: homeCellID)
        
        tableView.estimatedRowHeight = 200 //  预估高度
        tableView.rowHeight = UITableViewAutomaticDimension /// 自适应高度
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
    }
    
    private func setupNav() {
        
        // 1.左右按钮 注册/登录
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_friendattention", target: self, action: #selector(HomeViewController.leftItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_pop", target: self, action: #selector(HomeViewController.rightItemClick))
        
        // 2.初始化标题按钮 title
        let titleBtn = TitleButton()
        titleBtn.setTitle("babyWong ", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: #selector(HomeViewController.titleBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn

    }
    
    func leftItemClick() {
        
    }

    func rightItemClick() {
        
    }
    
    func titleBtnClick(btn: TitleButton) {
        btn.selected = !btn.selected
    }
    // MARK: 加载数据
    private func loadData() {
        Status.loadStatuses { (models, error) in
            if error == nil {
                self.statuses = models
                return
            }
            print(error)
        }
        
    }
    
    // MARK: delegate
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! StatusCell
        // 2.设置数据
        let status = statuses![indexPath.row]
//        cell.textLabel?.text = status.text
        cell.status = status
        return cell
        
    }
    
}
